package com.easondongh.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easondongh.business.domain.Provider;
import com.easondongh.system.constants.Constants;
import com.easondongh.business.domain.Customer;
import com.easondongh.business.mapper.CustomerMapper;
import com.easondongh.system.utils.StringUtils;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.business.vo.CustomerQueryVo;
import com.easondongh.common.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easondongh.business.service.CustomerService;

import java.util.List;

/**
 * @author EasonDongH
 * @date 2020/5/26 8:16
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService{
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageResult<Customer> getByPage(BasePageQueryVo<CustomerQueryVo> queryCondition) {
        IPage<Customer> page = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        CustomerQueryVo queryParams = queryCondition.getQueryParams();
        if (queryParams != null) {
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getCustomerName()), Customer.COL_CUSTOMER_NAME, queryParams.getCustomerName());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getConnectionPerson()), Customer.COL_CONNECTION_PERSON, queryParams.getConnectionPerson());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getPhone()), Customer.COL_PHONE, queryParams.getPhone());
        }
        IPage<Customer> iPage = this.customerMapper.selectPage(page, queryWrapper);
        return new PageResult<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public boolean enableById(Integer id) {
        Customer customer = new Customer();
        customer.setId(id);
        customer.setEnable(Constants.ENABLE);
        return this.customerMapper.updateById(customer) == 1;
    }

    @Override
    public boolean disableById(Integer id) {
        Customer customer = new Customer();
        customer.setId(id);
        customer.setEnable(Constants.DISABLE);
        return this.customerMapper.updateById(customer) == 1;
    }

    @Override
    public List<Customer> getAllEnableCustomer() {
        return this.customerMapper.getAllEnableCustomer();
    }
}
